<?php
namespace Home\Controller;
use Think\Controller;
use Think\Page;
class UserController extends CommonController {
	public function teamPic(){		
		$this -> assign('title','团队图谱');

		$mod = M('users') ;
		//判断用户搜索类型
		if(!empty(I('get.user_name'))){
			$user = $mod -> where('user_name = "%s" ',I('get.user_name')) ->find();
			$id = $user['user_id'];
		}else{
			if(empty(I('get.id'))){
				$id = $_SESSION['users']['user_id'];
				
			}else{
				$id = I('get.id');
				
			}
		}
		//传递当前最高等级
		$this -> assign('top_id',$_SESSION['users']['user_id']);
		$this -> assign('id',$id);
		
		$treeTop = $mod -> where('user_id = "%s" ',$id) -> find();
		$this -> assign('treeTop',$treeTop);
		//当前最高等级下的两人
		$treeMessage = $mod -> where('tree_id = "%s" ',$id) ->order('reg_time')-> limit(2)->select();
		//左边下面的两个人
		if(!empty($treeMessage)){
			//判断左边
			if(!empty($treeMessage[0])){
				$treeMessage_left = $mod -> where('tree_id = "%s" ',$treeMessage[0]['user_id']) ->order('reg_time')-> limit(2)->select();
			}else{
				$treeMessage_left = null;
			}
			//判断右边
			if(!empty($treeMessage[1])){
				//右边下面的两个人
				$treeMessage_right = $mod -> where('tree_id = "%s" ',$treeMessage[1]['user_id']) ->order('reg_time')-> limit(2)->select();
			}else{
				$treeMessage_right = null;
			}
		}else{
			$treeMessage = null;
			$treeMessage_left = null;
			$treeMessage_right = null;
		}
		
		
		//判断该用户下面是否有人以他为接点人
		if(!empty($treeMessage)){
			//判断左边
				if(!empty($treeMessage_left)){
					$this -> assign('treeMessage0',$treeMessage[0]);
						if(!empty($treeMessage_left[0])){
							$this -> assign('treeMessage2',$treeMessage_left[0]);
						}else{
							$this -> assign('treeMessage2',0);
						}
						if(!empty($treeMessage_left[1])){
							$this -> assign('treeMessage3',$treeMessage_left[1]);
						}else{
							$this -> assign('treeMessage3',0);
						}					
				}else{
					$this -> assign('treeMessage0',0);
					$this -> assign('treeMessage2',0);
					$this -> assign('treeMessage3',0);
				}
			//判断右边
				if(!empty($treeMessage_right)){
					$this -> assign('treeMessage1',$treeMessage[1]);
						if(!empty($treeMessage_right[0])){
							$this -> assign('treeMessage4',$treeMessage_right[0]);
						}else{
							$this -> assign('treeMessage4',0);
						}
						if(!empty($treeMessage_right[1])){
							$this -> assign('treeMessage5',$treeMessage_right[1]);
						}else{
							$this -> assign('treeMessage5',0);
						}			
				}else{
					$this -> assign('treeMessage1',0);
					$this -> assign('treeMessage4',0);
					$this -> assign('treeMessage5',0);
				}
		}else{
				$this -> assign('treeMessage0',0);
				$this -> assign('treeMessage1',0);
				$this -> assign('treeMessage2',0);
				$this -> assign('treeMessage3',0);
				$this -> assign('treeMessage4',0);
				$this -> assign('treeMessage5',0);
		}


		$this -> display();
	}
	public function register(){
		$this -> assign('title','注册会员');
		$id = I('get.id');
		$this -> assign('id',$id);
		$this -> display();
	}
	//判断手机号注册次数
	public function phoneAjax(){
		$mod = M('users');
		$num = $mod -> where('mobile_phone = "%s"',I('post.phone')) ->count();	
		if($num>10){
			echo '2';
		}else{
			if(I('post.mobile_code')==$_SESSION['phone']['code']){
				echo '1';
			}else{
				echo '22';
			}
		}
	}
	//账号激活
	public function jihuo(){
		$this -> assign('title','账号激活');
		$this -> display();
	}
	//获取手机验证码
	public function mobile_phoneAjax(){
			//判断推荐人和报单中心是否存在
		$mod = M('users');
			$res1 = $mod -> where('inviter_id = "%s" ',I('post.inviteruser')) -> find(); 
			$res2 = $mod -> where('report_user_id = "%s" ',I('post.reportuser')) -> find(); 
			if($res1>0 && $res2>0){
				$_SESSION['phone']['code'] = rand('111111','999999');
								$_SESSION['phone']['mobile'] =  I('post.mobile_phone');
								$_SESSION['phone']['content']=",您的短信验证码为".$_SESSION['phone']['code']."，仅用于注册会员，短信验证码的有效时间为5分钟，使用后即作废";
							
							$this -> send_msg();


			}else{
				echo '22';
			}
				
	}

	//处理注册会员信息
	//验证信息
	public function doregister(){

		$user = I('post.');
		//加入安全随机码
		$user['ec_salt'] = rand('1111','9999');
		$user['report_user_id'] = $user['reportuser'];
		$user['tree_id'] = $user['treeuser'];
		$user['inviter_id'] = $user['inviteruser'];
		$user['password'] = md5(md5($user['password1']).$user['ec_salt']);
		$user['repassword'] = md5($user['repassword1']);
		$user['reg_time'] = time();

		unset($user['password1']);unset($user['password2']);
		unset($user['repassword1']);unset($user['repassword2']);
		unset($user['reportuser']);unset($user['treeuser']);
		unset($user['inviteruser']);unset($user['mobile_code']);
		$mod = M('users');
		$res = $mod -> add($user);
		if($res>0){
			$this -> success('注册会员成功',U('home/user/teamPic'));
		}else{
			$this -> error('注册会员失败',U('home/user/register'));
		}

	}

	public function send_msg(){
				  
					//接口账号
					$uid = 'dong354698601';

					//登录密码
					$pwd = 'dong6395175346';

					/**
					* 实例化接口
					* 
					* @param string $uid 接口账号
					* @param string $pwd 接口密码					*/
				
					$api = new \Org\Util\Smsapi($uid,$pwd);

					$mobile = $_SESSION['phone']['mobile'];

					//短信内容参数
					$contentParam = array(
						'code'		=> $_SESSION['phone']['code'],
						'username'	=> $_SESSION['phone']['content']
						);

					//变量模板ID
					$template = '397816';

					//发送变量模板短信
					$result = $api->send($mobile,$contentParam,$template);

					if($result['stat']=='100')
					{
						echo '发送成功';
					}
					else
					{
						echo '发送失败:'.$result['stat'].'('.$result['message'].')';
					}

		}

 


}